package 队列;

public class 链式队列 implements  IQueue{
    Node front;//队首指示，表示头节点
    Node rear;//队尾指示，表示队尾节点
    int size;

    public 链式队列(){
        front = null;
        rear = null;
    }
    @Override
    public void append(Object obj) throws Exception {
        Node p = new Node(obj, null);
        if (front == null) {
            front = p;
            rear = p;
        } else {
            rear.next = p;
            rear = p;
        }
    }
    @Override
    public Object delete() throws Exception {
        if(front==rear){
            return null;
        }else{
            Node p = front.next;
            front.next=p.next;
            Object a = p.data;//获取首元节点的值
            if (p==rear){//归位
                rear=front;
            }
            return a;
        }
    }

    @Override
    public Object getFront() throws Exception {
        if(front==rear) {
            return null;
        }else{
            return front.next.data;
        }
    }

    @Override
    public boolean isEmpty() {
        return front==rear;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void print() throws Exception {
        Node current = front; // 从队首节点开始遍历
        while (current != null) {
            System.out.print(current.data + " "); // 打印当前节点的数据
            current = current.next; // 移动到下一个节点
        }
        System.out.println(); // 换行
    }
}
